package admin;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import user.User;
import user.UserHome;
import descriptor.AdminDescriptor;


/**
 * AdminBean
 * 
 * 
 * The AdminBean class implements all the methods that a remote client expect to find
 * when it calls the interface (Admin).
 * 
 * Note :  -We use @SuppressWarnings("unchecked") to avoid the useless warnings
 */
@SuppressWarnings("unchecked")
public abstract class AdminBean implements EntityBean{
	
	InitialContext initialContext;
	UserHome userHome;
	
	
	
	
	// <ATTRIBUTES>
	
	// simple attributes
	public abstract int getId();
	public abstract void setId(int id);
	
	public String getLogin(){
		return getUser().getLogin();
	}
	public void setLogin(String login){
		getUser().setLogin(login);
	}
	
	public String getPassword(){
		return getUser().getPasswd();
	}
	public void setPassword(String password){
		getUser().setPasswd(password);
	}
	
	public String getFirstname(){
		return getUser().getFirstName();
	}
	public void setFirstname(String firstname){
		getUser().setFirstName(firstname);
	}
	
	public String getSurname(){
		return getUser().getName();
	}
	public void setSurname(String surname){
		getUser().setName(surname);
	}
	
	public String getEmail(){
		return getUser().getMail();
	}
	public void setEmail(String email){
		getUser().setMail(email);
	}
	
	// Bean attributes
	// relations 1-1
	public abstract User getUser();
	public abstract void setUser(User user);
	
	
	// </ATTRIBUTES>
	
	public AdminDescriptor toAdminDescriptor(){
		return new AdminDescriptor(getId(),getLogin(),getPassword(),getFirstname(),getSurname(),getEmail());
	}
	
	
	
	
	// <BEANS METHODS>
	
	public String ejbCreate(String firstName,String name, String login,String passwd,String email) throws CreateException
	{
		try
		{
			initialContext = new InitialContext();
			userHome = (UserHome)initialContext.lookup("java:comp/env/ejb/User");
		}
		catch (NamingException e) 
		{
			e.printStackTrace();
		}
		User u = null;
		u = userHome.create(firstName, name, login, passwd, email);
		u.setType(User.ADMIN);
		setId(u.getId());
		return null;
	}
	
	public void ejbPostCreate(String firstName,String name, String login,String passwd,String email) 
	{
		User u = null;
		try 
		{
			u = userHome.findByPrimaryKey(getId());
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		}
		setUser(u);
	}
	
	public void ejbActivate() throws EJBException, RemoteException {}
	public void ejbPassivate() throws EJBException, RemoteException {}
	public void ejbRemove() throws RemoveException 
	{
		int id = getId();
		User u = null;
		try
		{
			initialContext = new InitialContext();
			userHome = (UserHome)initialContext.lookup("java:comp/env/ejb/User");
			u = userHome.findByPrimaryKey(id);
		} 
		catch (FinderException e) 
		{
			e.printStackTrace();
		} catch (NamingException e) 
		{
			e.printStackTrace();
		}
		try 
		{
			u.remove();
		} 
		catch (RemoveException e) 
		{
			e.printStackTrace();
		}
	}
	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {}
	public void ejbLoad() throws EJBException, RemoteException {}
	public void ejbStore() throws EJBException, RemoteException {}
	public void setEntityContext(EntityContext arg0) throws EJBException, RemoteException {}
	public void unsetEntityContext() throws EJBException, RemoteException {}
	
	// </ BEANS METHODS>
	
}
